package org.jeecg.modules.flowable.system.service.fs.impl;


import lombok.extern.slf4j.Slf4j;
import org.jeecg.modules.flowable.common.fs.FsDataResult;
import org.jeecg.modules.flowable.common.util.IdUtil;
import org.jeecg.modules.flowable.system.entity.TBaseSysFlEntity;
import org.jeecg.modules.flowable.system.entity.TBaseSysFsEntity;
import org.jeecg.modules.flowable.system.exception.LnchSystemException;
import org.jeecg.modules.flowable.system.mapper.TBaseSysFlMapper;
import org.jeecg.modules.flowable.system.mapper.TBaseSysFsMapper;
import org.jeecg.modules.flowable.system.service.fs.FsManagerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
@Slf4j
public class FsManagerImpl implements FsManagerService {

    private static ThreadLocal<Map<String, String>> fsMap = new ThreadLocal<>();

    @Autowired
    private TBaseSysFlMapper tBdSysFlMapper;
    @Autowired
    private TBaseSysFsMapper tBdSysFsMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String fileReg(String argFsId, Integer argSysFileType, FsDataResult argFsDataResult)
            throws LnchSystemException {
        TBaseSysFsEntity tsf = tBdSysFsMapper.selectById(argFsId);
        if (tsf == null) {
            log.error("未发现文件服务器，FS_ID=" + argFsId);
            throw new LnchSystemException("未发现文件服务器，ID=" + argFsId);
        }

        TBaseSysFlEntity obj = new TBaseSysFlEntity();
        obj.setId(IdUtil.getStringId());
        obj.setCreateTime(new Date());
        obj.setDelFlag(0);
        obj.setFileName(argFsDataResult.getFileName());
        obj.setFsSize(argFsDataResult.getSize());
        obj.setFsUri(argFsDataResult.getUri());
        obj.setOriFileName(argFsDataResult.getOriFileName());
        obj.setSysFileType(argSysFileType);
        obj.setSysFsId(argFsId);
        obj.setIsDelete(0);
        tBdSysFlMapper.insert(obj);
        return obj.getId();
    }

    @Override
    public String getUrl(String argFlId) throws LnchSystemException {
        TBaseSysFlEntity tsf = tBdSysFlMapper.selectById(argFlId);
        if (tsf == null) {
            log.debug("文件不存在，FL_ID=" + argFlId);
            throw new LnchSystemException("文件不存在");
        }
        TBaseSysFsEntity tss = tBdSysFsMapper.selectById(tsf.getSysFsId());
        if (tss == null) {
            log.debug("文件服务器不存在，FS_ID=" + tsf.getSysFsId());
            throw new LnchSystemException("文件服务器不存在");
        }

        return tss.getHttpAddr() + tsf.getFsUri();
    }

    @Override
    public String getUrl(String argFsId, String argUri, Boolean argIsFocus) {
        if (argFsId == null)
            return null;
        Map<String, String> stringStringMap = fsMap.get();
        if (stringStringMap == null || argIsFocus) {
            stringStringMap = new HashMap<>();
            List<TBaseSysFsEntity> tgCmFsList = tBdSysFsMapper.selectList(null);
            for (TBaseSysFsEntity tgCmFs : tgCmFsList) {
                stringStringMap.put(tgCmFs.getId(), tgCmFs.getHttpAddr());
            }
            fsMap.set(stringStringMap);
        }
        if (stringStringMap.get(argFsId) == null)
            return null;
        if (argUri == null)
            return null;
        return stringStringMap.get(argFsId) + argUri;
    }

    @Override
    public String getHttpAddrById(String fsId) {
        log.debug("非缓存加载文件配置[" + fsId + "]");
        TBaseSysFsEntity tsf = tBdSysFsMapper.selectById(fsId);
        if (tsf == null) {
            return "";
        }
        return tsf.getHttpAddr().trim();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Map<String, FsDataResult> fileReg(String argFsId, Map<FsDataResult, Integer> argFsDataResultAndFileType)
            throws LnchSystemException {
        TBaseSysFsEntity tsf = tBdSysFsMapper.selectById(argFsId);
        if (tsf == null) {
            log.error("未发现文件服务器，FS_ID=" + argFsId);
            throw new LnchSystemException("未发现文件服务器，ID=" + argFsId);
        }
        Map<String, FsDataResult> resultMap = new HashMap<String, FsDataResult>();
        Set<FsDataResult> set = argFsDataResultAndFileType.keySet();
        Iterator<FsDataResult> it = set.iterator();
        while (it.hasNext()) {
            FsDataResult fsDataResult = it.next();
            Integer sysFileType = argFsDataResultAndFileType.get(fsDataResult);
            TBaseSysFlEntity obj = new TBaseSysFlEntity();
            obj.setId(IdUtil.getStringId());
            obj.setCreateTime(new Date());
            obj.setIsDelete(0);
            obj.setFileName(fsDataResult.getFileName());
            obj.setFsSize(fsDataResult.getSize());
            obj.setFsUri(fsDataResult.getUri());
            obj.setOriFileName(fsDataResult.getOriFileName());
            obj.setSysFileType(sysFileType);
            obj.setSysFsId(argFsId);
            obj.setIsDelete(0);
            tBdSysFlMapper.insert(obj);
            resultMap.put(obj.getId(), fsDataResult);
        }
        return resultMap;
    }
}
